package me.yangzhi.day_08.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.yangzhi.day_08.R;
import me.yangzhi.day_08.model.Merchant;
import me.yangzhi.day_08.adapter.MerchantAdapter;
import me.yangzhi.day_08.adapter.BannerAdapter;

/**
 * HomeFragment 是首页的Fragment
 * 主要功能：
 * 1. 显示搜索栏
 * 2. 显示轮播图
 * 3. 显示分类导航
 * 4. 显示商家列表
 */
public class HomeFragment extends Fragment {

    // 轮播图ViewPager
    private ViewPager2 bannerViewPager;
    // 商家列表RecyclerView
    private RecyclerView merchantList;
    // 商家列表适配器
    private MerchantAdapter merchantAdapter;
    // 分类网格
    private GridLayout categoryGrid;
    private BannerAdapter bannerAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载Fragment的布局
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        
        // 初始化视图
        initViews(root);
        // 设置轮播图
        setupBanner();
        // 设置商家列表
        setupMerchantList();
        
        return root;
    }

    /**
     * 初始化视图组件
     * @param root Fragment的根视图
     */
    private void initViews(View root) {
        bannerViewPager = root.findViewById(R.id.bannerViewPager);
        merchantList = root.findViewById(R.id.merchantRecyclerView);
        categoryGrid = root.findViewById(R.id.categoryGrid);
        
        // 设置分类图标和文字
        setupCategories();
    }

    /**
     * 设置分类导航
     * 包括图标和文字的设置
     */
    private void setupCategories() {
        // 分类图标资源ID数组
        int[] categoryIcons = {
            R.drawable.ic_takeout,
            R.drawable.ic_food,
            R.drawable.ic_hotel,
            R.drawable.ic_movie,
            R.drawable.ic_more
        };
        
        // 分类名称数组
        String[] categoryNames = {
            "外卖",
            "美食",
            "酒店",
            "电影",
            "更多"
        };

        // 遍历添加分类项
        for (int i = 0; i < categoryNames.length; i++) {
            View categoryView = getLayoutInflater().inflate(R.layout.item_category, categoryGrid, false);
            
            ImageView icon = categoryView.findViewById(R.id.categoryIcon);
            TextView name = categoryView.findViewById(R.id.categoryName);
            
            icon.setImageResource(categoryIcons[i]);
            name.setText(categoryNames[i]);

            // 设置GridLayout的布局参数
            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.width = 0;
            params.height = GridLayout.LayoutParams.WRAP_CONTENT;
            params.columnSpec = GridLayout.spec(i % 5, 1f);
            params.rowSpec = GridLayout.spec(i / 5);
            categoryView.setLayoutParams(params);
            
            categoryGrid.addView(categoryView);
        }
    }

    /**
     * 设置轮播图
     * 待实现：添加轮播图适配器和数据
     */
    private void setupBanner() {
        bannerAdapter = new BannerAdapter();
        bannerViewPager.setAdapter(bannerAdapter);
        
        // 设置图片资源
        List<Integer> bannerImages = Arrays.asList(
            R.drawable.banner_lobster,  // 小龙虾图片
            R.drawable.banner_meat,     // 炒肉图片
            R.drawable.banner_goose     // 烤鹅图片
        );
        
        bannerAdapter.setBannerImages(bannerImages);
        
        // 设置无限循环
        bannerViewPager.setCurrentItem(1, false);
        
        // 设置页面切换效果
        bannerViewPager.setOffscreenPageLimit(1);
        bannerViewPager.setPageTransformer((page, position) -> {
            float absPos = Math.abs(position);
            page.setAlpha(1 - absPos * 0.5f);
            page.setScaleX(0.85f + 0.15f * (1 - absPos));
            page.setScaleY(0.85f + 0.15f * (1 - absPos));
        });
    }

    /**
     * 设置商家列表
     * 1. 设置布局管理器
     * 2. 设置适配器
     * 3. 添加测试数据
     */
    private void setupMerchantList() {
        merchantList.setLayoutManager(new LinearLayoutManager(requireContext()));
        merchantAdapter = new MerchantAdapter();
        merchantList.setAdapter(merchantAdapter);

        // 添加测试数据
        List<Merchant> merchants = new ArrayList<>();
        merchants.add(new Merchant(
            "麻辣小龙虾",
            "4.8",
            "月售2000+",
            "起送¥30 | 配送¥5",
            R.drawable.banner_lobster
        ));
        merchants.add(new Merchant(
            "川味炒肉",
            "4.6",
            "月售1500+",
            "起送¥20 | 配送¥4",
            R.drawable.banner_meat
        ));
        merchants.add(new Merchant(
            "潮汕白切鹅",
            "4.7",
            "月售1800+",
            "起送¥25 | 配送¥5",
            R.drawable.banner_goose
        ));
        merchants.add(new Merchant(
            "粤式烧腊",
            "4.5",
            "月售1200+",
            "起送¥28 | 配送¥6",
            R.drawable.banner_goose
        ));
        merchants.add(new Merchant(
            "麻辣烫",
            "4.4",
            "月售2500+",
            "起送¥15 | 配送¥3",
            R.drawable.banner_meat
        ));
        
        merchantAdapter.setMerchants(merchants);
    }
} 